package com.xyz.keshe.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.xyz.keshe.db.DatabaseHelper;
import com.xyz.keshe.model.Announcement;

import java.util.ArrayList;
import java.util.List;

public class AnnouncementDao {
    private final DatabaseHelper dbHelper;

    public AnnouncementDao(Context context) {
        this.dbHelper = new DatabaseHelper(context);
    }

    // 添加公告
    public long addAnnouncement(Announcement announcement) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DatabaseHelper.COLUMN_ANNOUNCEMENT_TITLE, announcement.getTitle());
        values.put(DatabaseHelper.COLUMN_ANNOUNCEMENT_CONTENT, announcement.getContent());

        long id = db.insert(DatabaseHelper.TABLE_ANNOUNCEMENT, null, values);
        db.close();
        return id;
    }

    // 插入公告（别名方法）
    public long insertAnnouncement(Announcement announcement) {
        return addAnnouncement(announcement);
    }

    // 更新公告
    public int updateAnnouncement(Announcement announcement) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DatabaseHelper.COLUMN_ANNOUNCEMENT_TITLE, announcement.getTitle());
        values.put(DatabaseHelper.COLUMN_ANNOUNCEMENT_CONTENT, announcement.getContent());
        values.put(DatabaseHelper.COLUMN_UPDATE_TIME, "CURRENT_TIMESTAMP");

        int rowsAffected = db.update(
                DatabaseHelper.TABLE_ANNOUNCEMENT,
                values,
                DatabaseHelper.COLUMN_ID + " = ?",
                new String[]{String.valueOf(announcement.getId())}
        );
        db.close();
        return rowsAffected;
    }

    // 删除公告
    public int deleteAnnouncement(int id) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        int rowsAffected = db.delete(
                DatabaseHelper.TABLE_ANNOUNCEMENT,
                DatabaseHelper.COLUMN_ID + " = ?",
                new String[]{String.valueOf(id)}
        );
        db.close();
        return rowsAffected;
    }

    // 获取所有公告
    public List<Announcement> getAllAnnouncements() {
        List<Announcement> announcementList = new ArrayList<>();
        String selectQuery = "SELECT * FROM " + DatabaseHelper.TABLE_ANNOUNCEMENT + 
                             " ORDER BY " + DatabaseHelper.COLUMN_CREATE_TIME + " DESC";

        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        if (cursor.moveToFirst()) {
            do {
                Announcement announcement = new Announcement();
                announcement.setId(cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_ID)));
                announcement.setTitle(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_ANNOUNCEMENT_TITLE)));
                announcement.setContent(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_ANNOUNCEMENT_CONTENT)));
                announcement.setCreateTime(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_CREATE_TIME)));
                announcement.setUpdateTime(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_UPDATE_TIME)));

                announcementList.add(announcement);
            } while (cursor.moveToNext());
        }

        cursor.close();
        db.close();
        return announcementList;
    }

    // 通过ID获取公告
    public Announcement getAnnouncementById(int id) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(
                DatabaseHelper.TABLE_ANNOUNCEMENT,
                null,
                DatabaseHelper.COLUMN_ID + " = ?",
                new String[]{String.valueOf(id)},
                null, null, null
        );

        Announcement announcement = null;
        if (cursor != null && cursor.moveToFirst()) {
            announcement = new Announcement();
            announcement.setId(cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_ID)));
            announcement.setTitle(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_ANNOUNCEMENT_TITLE)));
            announcement.setContent(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_ANNOUNCEMENT_CONTENT)));
            announcement.setCreateTime(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_CREATE_TIME)));
            announcement.setUpdateTime(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_UPDATE_TIME)));
            cursor.close();
        }
        db.close();
        return announcement;
    }
} 